﻿using MediatR;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using Share;
using SystemHYS.API.Application.command.ProductionOrdersCommand;
using SystemHYS.Domian;
using SystemHYS.Infrstructre.Imp;
using SystemHYS.Infrstructre;
using SystemHYS.Infrstructre.Interface;
using static Share.ParseAPI;
using Microsoft.EntityFrameworkCore;
namespace SystemHYS.API.Application.commandler.ProductionOrdersCommandler
{
    /// <summary>
    /// 生产订单添加
    /// </summary>
    public class ProductionOrdersCreateCommandler:IRequestHandler<ProductionOrdersCreateCommand,int>
    {
        IProductionOrdersRepository OrdersRepository;
        IProductionOrdersFinishedProductRepository ProductionOrdersFinishedProductRepository;
        public SystemCoretext db;
        public ProductionOrdersCreateCommandler(IProductionOrdersRepository ordersRepository, IProductionOrdersFinishedProductRepository productionOrdersFinishedProductRepository, SystemCoretext db)
        {
            OrdersRepository = ordersRepository;
            ProductionOrdersFinishedProductRepository = productionOrdersFinishedProductRepository;
            this.db = db;
        }

        public async Task<int> Handle(ProductionOrdersCreateCommand request, CancellationToken cancellationToken)
        {
            
            using (var tran = await db.Database.BeginTransactionAsync(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    var list = new ProductionOrders()
                    {
                        ProductionOrdersId = 0,
                        ProductionNumber = request.ProductionNumber,
                        Client = request.Client,
                        OrderNumber = request.OrderNumber,
                        Priority = request.Priority,
                        remark=request.remark,
                        State=false,
                        AddName = "李明",
                        AddTime = request.AddTime.Value.ToUniversalTime(),
                    };
                    await OrdersRepository.AddAsyn(list);
                    var id = list.ProductionOrdersId;
                    foreach (var item in request.list)
                    {
                        await ProductionOrdersFinishedProductRepository.AddAsyn(new ProductionOrdersFinishedProduct()
                        {
                            ProductionOrdersId = id,
                            FinishedProductId = item.finishedProductId,
                            AddName = "李明",
                            AddTime = DateTime.Now.ToUniversalTime(),
                            Plannedproduction=item.Plannedproduction,
                            Deliverydate=item.Deliverydate.ToUniversalTime()
                        });
                    }
                    await tran.CommitAsync();
                    return 1;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
        

    }
}
